using System;
using System.Collections.Generic;
using System.Windows.Forms;
using CosyTrade.Client.Properties;
using CosyTrade.Organization;
using CosyTrade.Security;
using CosyTrade.UI;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;

namespace CosyTrade.Client
{
    public partial class LoginForm : XtraForm
    {
        private readonly ISecuritySystem securitySys;
        private readonly IOrganizationManager organizationMgr;
        private readonly ISystemStatCenter statCenter;

        public LoginForm(ISecuritySystem securitySys, IOrganizationManager organizationMgr, ISystemStatCenter statCenter)
        {
            this.securitySys = securitySys;
            this.organizationMgr = organizationMgr;
            this.statCenter = statCenter;
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (Functions.ValueIsEmpty(txtAccount))
            {
                Functions.ShowMessageBox(Resources.LoginForm_PleaseInputAccount);
                return;
            }
            if (Functions.ValueIsEmpty(txtPassword))
            {
                Functions.ShowMessageBox(Resources.LoginForm_PleaseInputPassword);
                return;
            }

            if (cmbCompanies.SelectedItem == null)
            {
                Functions.ShowMessageBox(Resources.LoginForm_PleaseSelectCompany);
                return;
            }
            Company loginCompany = ((ImageComboBoxItem) cmbCompanies.SelectedItem).Value as Company;
            if (loginCompany == null)
            {
                Functions.ShowMessageBox(Resources.LoginForm_PleaseSelectCompany);
                return;
            }


            User loginUser = securitySys.GetUserDetail(txtAccount.Text.Trim());
            if (loginUser != null)
            {
                if (User.Encode(txtPassword.Text.Trim()) == loginUser.Password)
                {
                    Employee employee = organizationMgr.GetEmployeeDetailByRoleId(loginUser.MapToRole.PrimaryKey);
                    if (employee == null)
                    {
                        Functions.ShowMessageBox(Resources.LoginForm_NoMapEmployee);
                        return;
                    }
                    if (employee.OnCompany.PrimaryKey != loginCompany.PrimaryKey &&
                        !loginUser.MapToRole.CanAssessChildsCompany)
                    {
                        Functions.ShowMessageBox(Resources.LoginForm_NoAccess);
                        return;
                    }
                    statCenter.CreateSystemStat(loginCompany, employee, loginUser);
                    DialogResult = DialogResult.OK;
                    return;
                }
            }
            Functions.ShowMessageBox(Resources.LoginForm_LoginAuthFaild);
        }


        private void LoginForm_Load(object sender, EventArgs e)
        {
            IList<Company> companyList = organizationMgr.GetCompanyList();
            foreach (Company company in companyList)
                cmbCompanies.Properties.Items.Add(new ImageComboBoxItem(company.Caption, company, -1));
        }
    }
}